﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace InCom.OTM.Adapters
{
    /// <summary>
    /// 数据同步初始化功能 适配器
    /// </summary>
    public class SyncStatusAdapter
    {
        /// <summary>
        /// 用于外部可访问的单例
        /// </summary>
        private static readonly SyncStatusAdapter instance = new SyncStatusAdapter();

        /// <summary>
        /// 用于外部可访问的单例
        /// </summary>
        public static SyncStatusAdapter Instance
        {
            get { return instance; }
        }

        #region 数制同步启动功能

        /// <summary>
        /// 开始数据同步功能
        /// </summary>
        public void StartSyncStatus()
        {
            //处理协议解析加载
            InCom.API.Protocols.PluginModulesManager.Instance.BaseAssembly.Add(typeof(InCom.API.Protocols.Parses.Closed).Assembly);

            InCom.API.Protocols.PluginModulesManager.Instance.InitFullDic();
            //处理数据同步配置,从本地加载
            InCom.SyncStatus.SyncStatusConfigManager.Instance.LoadConfig();
            //设置当前节点类型
            InCom.SyncStatus.SyncStatusConfigManager.Instance.LocalPointType = SyncStatus.SyncPointType.InCom;
            //设置数据操作功能接口
            //InCom.SyncStatus.TerminalManager.Instance.DbOperation = DataProvider.DbProvider.Create();
            //加载终端数据对像
            //InCom.SyncStatus.TerminalManager.Instance.LoadTerminal();
            #region 初始化终端数据
            //初始化数据加载
            //var items = DataProvider.DbProvider.Create().QueryCarBasicInfor();
            //foreach (var item in items)
            //{
            //    if (InCom.SyncStatus.TerminalManager.Instance.Find(item.TerminalCode) == null)
            //    {
            //        InCom.SyncStatus.TerminalManager.Instance.Add(item.TerminalCode);
            //    }
            //}
            #endregion

            //用于处理数据同步节点的Handler
            InCom.SyncStatus.SyncPointManager.Instance.SyncProtocolHandler = InCom.Core.SyncInteractive.SyncDataInteractiveSupervise.Instance.ProtocolProcess;
            //开始监听服务
            var tasksync = Task.Factory.StartNew(() =>
            {
                //开启数据同步功能
                InCom.SyncStatus.SyncPointManager.Instance.Start();
            });
            //incom 数制同步的重联机制,5分钟后重新联接
            var timer = new System.Timers.Timer();
            //1分钟执行一次联接检查.
            timer.Interval = 1000 * 60;
            //重新联接
            timer.Elapsed += (s, e) =>
            {
                timer.Stop();
                try
                {
                    //incom不进行主动联接其它节点
                    InCom.SyncStatus.SyncPointManager.Instance.StartClientConnections();
                }
                catch (Exception ex)
                {
                    inCom.Logs.LogProvider.Create().Write(inCom.Logs.EventType.Warning, "联接数据同步节点失败 " + ex.ToString());
                }
                timer.Start();
            };
            timer.Start();
        }
        /// <summary>
        /// 测试运行时间
        /// </summary>
        /// <param name="method">需要执行的方法</param>
        /// <returns>返回,这个方法执行用了多少毫秒</returns>
        public long Stopwatch(Action method)
        {
            var watch = new System.Diagnostics.Stopwatch();
            watch.Restart();
            //运行方法
            method.Invoke();
            watch.Stop();
            return watch.ElapsedMilliseconds;
        }
        #endregion
    }
}
